package com.zs.leymovie.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.zs.leymovie.MainActivity;
import com.zs.leymovie.R;
import com.zs.leymovie.activity.SelectActivity;
import com.zs.leymovie.bean.MovieShowing;
import com.zs.leymovie.utils.EventBusHelp;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2015/8/18 0018.
 */
public class MovingAdapter extends AppAdapter<MovieShowing> {
    private Context context;
    private List<MovieShowing> list;
    private BitmapDisplayConfig config;
    private final Animation animation;
    private BitmapUtils bitmapUtils;

    public MovingAdapter(List<MovieShowing> list, Context context) {
        super(list, context);
        this.list = list;
        this.context = context;
        config = new BitmapDisplayConfig();
        config.setLoadFailedDrawable(context.getResources().getDrawable(R.mipmap.huodng_image));
        config.setLoadingDrawable(context.getResources().getDrawable(R.mipmap.huodng_image));
        animation = AnimationUtils.loadAnimation(
                context, R.anim.home_listview_anim);
        config.setAnimation(animation);
        bitmapUtils = new BitmapUtils(context);
    }

    @Override
    public View getItemView(final int position, View convertView, ViewGroup parent) {
        ViewHolder vh;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.movie_base_item_lv, parent, false);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        bitmapUtils.display(vh.movie_img, list.get(position).getMovie_img_url(), config);
        vh.movie_name.setText(list.get(position).getMovie_name());
        vh.movie_presale.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new EventBusHelp(position, list.get(position).getMovie_id(), list.get(position).getMovie_name()));
                Intent intent = new Intent(context, SelectActivity.class);
                intent.putExtra("city_id", ((MainActivity)context).getCity_id());
                intent.putExtra("movie_id", list.get(position).getMovie_id());
                intent.putExtra("movie_name", list.get(position).getMovie_name());
                context.startActivity(intent);
            }
        });
        if (TextUtils.equals(list.get(position).getMovie_presale(), "0")) {
            vh.movie_cast.setVisibility(View.INVISIBLE);
            vh.movie_desc.setText(list.get(position).getMovie_desc());
            vh.movie_score.setText(list.get(position).getMovie_score());
            vh.movie_presale.setText("购票");
            vh.movie_presale.setBackgroundColor(context.getResources().getColor(R.color.red));
            vh.show_num.setText("有" + list.get(position).getCinema_num() + "家影院上映" + list.get(position).getShow_num() + "场");
        } else if (TextUtils.equals(list.get(position).getMovie_presale(), "1")) {
            vh.movie_cast.setVisibility(View.VISIBLE);
            vh.movie_cast.setText("主演：" + list.get(position).getMovie_cast());
            vh.movie_score.setText(list.get(position).getMovie_want_see_num() + "想看");
            vh.movie_desc.setText("导演:" + list.get(position).getMovie_director());
            vh.movie_presale.setText("预售");
            vh.movie_presale.setBackgroundColor(context.getResources().getColor(R.color.darkturquoise));
            vh.show_num.setText(list.get(position).getMovie_show_date() + "上映");
        }
        return convertView;
    }


    private class ViewHolder {
        @ViewInject(R.id.movie_img)
        private ImageView movie_img;
        @ViewInject(R.id.movie_name)
        private TextView movie_name;
        @ViewInject(R.id.movie_score)
        private TextView movie_score;
        @ViewInject(R.id.movie_desc)
        private TextView movie_desc;
        @ViewInject(R.id.movie_cast)
        private TextView movie_cast;
        @ViewInject(R.id.movie_presale)
        private TextView movie_presale;
        @ViewInject(R.id.show_num)
        private TextView show_num;


        public ViewHolder(View convertView) {
            ViewUtils.inject(this, convertView);
        }
    }
}
